<?php
/**
 * Created by PhpStorm.
 * User: 17274
 * Date: 2018/3/28
 * Time: 14:21
 */

namespace app\admin\controller;


use app\admin\model\AlbumModel;
use app\admin\model\LabelsModel;
use app\admin\model\VideoModel;
use think\Request;
use think\Config;

class Video extends Base
{
    protected $videoModel;
    protected $labelsModel;
    protected $labelsMaps;

    /*
     * 依赖注入Model
     * */
    public function __construct(VideoModel $videoModel,LabelsModel $labelsModel){
        parent::__construct();
        $this->videoModel = $videoModel;
        $this->labelsModel = $labelsModel;
        $this->labelsMaps=[
            "status" => 1,
            "category" => 'video'
        ];
    }

    /*
    * 列表
    * */
    public function video_index__label(){

    }

    /*
     * 列表
     * */
    public function video_index(){
        $label=input('get.code/s');
        $this->assign("code",$label);
        $this->assign("data",$this->videoModel->listAll());
        return $this->fetch('video/index');
    }

    /*
    * 新增
    * */
    public function video_add(){
        if(Request::instance()->isPost()){
            $file = request()->file('video');
            if($file){
                $r_path=ROOT_PATH . 'public' . DS . 'uploads/video/'.input('post.label/s').'/';
                $r_url='/uploads/video/'.input('post.label/s').'/';
                $info = $file->validate(['ext'=>'mkv,mp4,avi,rm,rmvb'])->rule('uniqid')->move($r_path);
                if($info){
                    $data=input('post.');
                    $data['path']=$r_path.$info->getSaveName();
                    $data['url']=$r_url.$info->getFilename();
                    unset($data['video']);
                    unset($data['__token__']);
                    $validate=$this->videoModel->validate_model('add',$data);
                    if ($validate['code']){
                        return $this->videoModel->add($data);
                    }
                    return $validate;
                }
            }
            return op_fail();
        }
        $this->assign("label",input('get.code/s'));
        $this->assign("labels",$this->labelsModel->getByMaps($this->labelsMaps));
        return $this->fetch('video/add');
    }

    /*
    * 修改
    * */
    public function video_edit(){
        if(Request::instance()->isPost()){
            $data=input('post.');
            $file = request()->file('video');
            if($file){
                $r_path=ROOT_PATH . 'public' . DS . 'uploads/video/'.input('post.label/s').'/';
                $r_url='/uploads/video/'.input('post.label/s').'/';
                $info = $file->validate(['ext'=>'mkv,mp4,avi,rm,rmvb'])->rule('uniqid')->move($r_path);
                if($info){
                    $data['path']=$r_path.$info->getSaveName();
                    $data['url']=$r_url.$info->getFilename();
                }
            }
            unset($data['video']);
            unset($data['id']);
            unset($data['__token__']);
            $validate=$this->videoModel->validate_model('edit',$data);
            if ($validate['code']){
                return $this->videoModel->edit($data);
            }
            return $validate;
        }
        $id=input('get.id/d');
        $this->assign("labels",$this->labelsModel->getByMaps($this->labelsMaps));
        $this->assign("data",$this->videoModel->getById($id));
        return $this->fetch('video/edit');
    }

    /*
    * 删除
    * */
    public function video_del(){
        return $this->videoModel->del();
    }


}